package com.huxin.servelt;

import com.huxin.pojo.BookTypeInfo;
import com.huxin.pojo.BooksInfo;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/delectbooktype")
public class DelectBookTypeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求方法
        request.setCharacterEncoding("utf-8");
        //处理响应乱码
        response.setContentType("text/json;charset=utf-8");
        //获取请求参数
        String id=request.getParameter("id");
        //调用service
        Userservice service = new Userservice();
        BookTypeInfo bookTypeInfo=service.selectBooksTypeName(id);

        List<BooksInfo> booksInfoList=service.selectBooksByTpyeName(bookTypeInfo.getName());
        if (booksInfoList.size()==0){//能删除
         int i=service.delectbooktype(id);
            if(i>0){
                response.getWriter().write("true");
            }
        }else {//不能删除
            response.getWriter().write("false");
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
